// pages/uploadimg/uploadimg.js
const app = getApp();
var ea  = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    delete:app.data.imgurl +"delete.png",
    add:app.data.imgurl +"addshi.png",
    videosdd: app.data.imgurl + "2017123101.mp4",
    array: ["风景", "人物", "时光"],
    index: 0,
    rightarrow:app.data.imgurl +"rightarrow.png",
    shuju: { xiazai: 0, videos:'',bisai:0},
    getclass: app.data.url + 'sheyingjia/Xzuoping1/getclasslist',
    getqiniutioken: app.data.url + 'sheyingjia/Xuploas1/gettoken',
    addenclosure: app.data.url + 'sheyingjia/Xuploas1/addenclosure',
    addvideozuopin: app.data.url + 'sheyingjia/Xzuoping1/addvideozuopin',
    checkeds:false,
    fenmian:'',
    xianshi:false,
    jindu:0,
    videos:'',
  },
  //点击阅读授权协议
  xuanze:function(e){
    var checkeds = this.data.checkeds;
    if (checkeds){
      checkeds = false;
    }else{
      checkeds = true;
    }
    this.setData({ checkeds: checkeds});
  },
  //获取数据
  xiugaishuju:function(e){
    console.log(e);
    var type = e.currentTarget.dataset.type;
    var value = e.detail.value;
    var shuju = this.data.shuju;
    shuju[type] = value;
    console.log(shuju);
    this.setData({shuju:shuju});
  },
  //选择分类
  bindPickerChange: function (e) {
    // console.log(e.detail.value)
    var index = e.detail.value;
    var array = this.data.array;
    var shuju = this.data.shuju;
    shuju.fenlei = array[index].id;
    console.log(shuju);
    this.setData({
      index: index,
      shuju:shuju
    })
  },

  //  选择视频
  choosevideo: function () {
    var that = this;
    
    wx.setKeepScreenOn({
      keepScreenOn: true
    })
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      camera: 'back',
      compressed:false,
      success: function (res) {
       console.log(res);
        var tempFilePath = res.tempFilePath;
        var fenmian = res.thumbTempFilePath;
        that.setData({ fenmian: fenmian, xianshi:true});
        if (!tempFilePath){
          return;
        }
        wx.showLoading({
          title: '上传中',
        })
        ea.upfile([tempFilePath], function (res,key) { 
          ea.request(that.data.addenclosure,{name:key},function(ress){
            wx.hideLoading();
            console.log(ress);
            var data = ress.data;
            if(data.code==200){
              console.log(data);
              var shuju = that.data.shuju;
              shuju.videos = data.data[0].id;
              that.setData({ jindu: 0, shuju: shuju, videos:data.data[0].path});
            
            }else{
              wx.showToast({
                title: data.msg,
                icon:'none'
              })
            }
          })
        }, that.data.getqiniutioken,'SCN',function(e){
          // console.log(e);
          var progress = e.progress;
          if (progress==100){
            progress = 99;
          }
          that.setData({ jindu: progress});
        })
       

      }
    })
  },
//发布作品
  fabuzuopin:function(){
    var that = this;
    var shuju = this.data.shuju;
    var user = wx.getStorageSync('user');
    if(!user){
      wx.showToast({
        title: '请先登录',
        icon:'none'
      })
      return;
    }
    var checkeds = this.data.checkeds;
    if (!checkeds){
      wx.showToast({
        title: '请先点击阅读',
        icon:'none'
      })
      return;
    }
    shuju.token = user.token;
    ea.request(that.data.addvideozuopin,shuju,function(res){
      var data = res.data;
      console.log(data);
      // return
      if(data.code==200){
        wx.showToast({
          title: data.msg,
        })
        setTimeout(function(){
          wx.navigateBack({
            delta: 1,
            fail: function () {
              wx.switchTab({
                url: '/pages/upload/upload',
              })
            }
          })
        }, app.data.times);
      }else{
        wx.showToast({
          title: data.msg,
          icon:'none'
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var bisai = options.bisai;
    var shuju = this.data.shuju;
    var that = this;
    if(bisai){
      shuju.bisai = bisai;
    }else{
      shuju.bisai = 0;
    }
    that.setData({shuju:shuju});
    ea.request(that.data.getclass, { bisai:shuju.bisai},function(res){
      var data  = res.data;
      if(data.code==200){
        var shuju = that.data.shuju;
        shuju.fenlei = data.data[0].id;
        console.log(shuju);
        that.setData({ array: data.data, shuju: shuju});
      }else{
        wx.showToast({
          title: data.msg,
          icon:'none'
        })
      }
    })
    wx.getSystemInfo({
      success: function(res) {
        console.log(res.model);
        var shuju = that.data.shuju;
        shuju.zhuangbei = res.model;
        console.log(shuju);
        that.setData({shuju:shuju});
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})